% specialedgedetection is a way to reduce the bad and unnecessary pixels
% taken from a previous edge detection.
% After the first detection, there are many unwanted edges left. We can
% reduce these edges by checking whether the pixels on the left of it is
% black and the right is white and vice versa.
function mask=filter_edge(bw,original,threshold)
    [sx,sy] = size(bw);
    [x,y] = find(bw);
    width = floor(sy/15);
    mask = zeros(sx,sy);
    for i=1:length(x)
        r = x(i);
        c = y(i);
        try
            t = finddiff(original,r,c,width);
            mask(r,c) = t;
        catch
            continue
        end
    end
    
    mask = mask/max(mask(:));
    mask = im2bw(mask,threshold);
end

% A helper for specialedgedetection
function value=finddiff(I,r,c,width)
    left = sum(sum(I(r,c-width:c)));
    right = sum(sum(I(r,c:c+width)));
    value = abs(right-left);
end